package com.jianbing.util;

import com.alibaba.fastjson.JSON;
import com.jianbing.entity.*;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Han, Tixiang on 2016/9/10 0010.
 */
public class TaskHelper {
    public static final int NOTIFY_SUCCESS = 1, NOTIFY_FAIL = -1;
    public static int LOGIN_SUCCESS = 1, PERSONAL_INFO = 2, YANGLAO_DATA = 3, YILIAO_DATA = 4, GONGSHANG_DATA = 5, SHIYE_DATA = 6, SHENGYU_DATA = 7;    //* 1: 登录成功 2：个人基本信息 3：养老数据4：医疗数据 5：工伤数据 6：失业数据7：生育数据
    public static int PRE_NEW = -2, NEW = 0, PROCESS = 1, SUCCESS = 2, FAIL = -1, DISCARD = 3;       //预先创建taskId,这个状态不需要向模拟器发送, 新建,处理中，成功，失败,舍弃//-1代表信息抓取失败或者因用户输入信息有误导致无法登录并获取数据；0代表该任务未被分配，1代表该任务分配且尚未有信息返回，2代表信息抓取成功。
    public static String FAYUAN_RUN_COMMAND = "run",FAYUAN_STOP_COMMAND="stop"; //法院启动，法院停止
    public static Charset UTF_8 = Charset.forName("UTF-8");

    /**
     * notify
     */
    public static String TAG_SHEBAO_NOTIFY = "sb_notify";  //社保notify tag
    public static String TAG_SHEBAO_YZM_NOTIFY = "sb_yzm_notify";     //验证码notify tag

    public static String TAG_GONGJIJIN_NOTIFY = "gjj_notify";    //公积金notify tag
    public static String TAG_GONGJIJIN_YZM_NOTIFY = "gjj_yzm_notify";     //验证码notify tag

    public static String TAG_GONGJIJIN_LOAD_NOTIFY = "gjj_loan_notify";
    public static String TAG_GONGJIJIN_LOAD_YZM_NOTIFY = "gjj_loan_yzm_notify";

    /***
     * 法院
     */

    public static String TAG_FAYUAN = "fayuan";
    public static String TAG_FAYUAN_NOTIFY = "fayuan_notify";
    /**
     * tag
     */
    public static String TAG_SHEBAO = "shebao";
    public static String TAG_SHEBAO_IE = "shebao_ie";
    public static String TAG_SHEBAO_YZM = "sb_yzm";

    public static String TAG_GONGJIJIN = "gjj";
    public static String TAG_GONGJIJIN_IE = "gjj_ie";
    public static final String TAG_GONGJIJIN_LOAN_IE = "gjj_loan_ie";
    public static String TAG_GONGJIJIN_YZM = "gjj_yzm";

    public static String TAG_GONGJIJIN_LOAD = "gjj_loan";
    public static String TAG_GONGJIJIN_LOAD_YZM = "gjj_loan_yzm";

    public static String TJ_GONGJIJIN = "tjgjj";

    public static int defaultTaskTimeout = 60 * 5;  //task默认超时时长，单位秒
    public static final String defaultConsumeThreadNums = "8";
    public static final String GJJ_IE_CITYIDS = "gjj_ie_cityids";
    public static final String GJJ_LOAN_IE_CITYIDS = "gjj_loan_ie_cityids";
    public static final String SHEBAO_IE_CITYIDS = "shebao_ie_cityids";

    public static Map<String, String> processMonitorCityTypeRelationShip = new HashMap<String, String>();

    static {
        String gjj_ie_cityids = PropertiesUtils.getProperties(TaskHelper.GJJ_IE_CITYIDS, "");

        if (StringUtils.isNotEmpty(gjj_ie_cityids)) {
            String[] gjj_ie_cityidsArray = gjj_ie_cityids.split(",");

            for (String gjj_ie : gjj_ie_cityidsArray) {
                processMonitorCityTypeRelationShip.put(TaskHelper.TAG_GONGJIJIN + gjj_ie.trim(), TaskHelper.TAG_GONGJIJIN_IE);
            }
        }

        String gjj_loan_ie_cityids = PropertiesUtils.getProperties(TaskHelper.GJJ_LOAN_IE_CITYIDS, "");

        if (StringUtils.isNotEmpty(gjj_loan_ie_cityids)) {
            String[] gjj_loan_ie_cityidsArray = gjj_loan_ie_cityids.split(",");

            for (String gjj_loan_ie : gjj_loan_ie_cityidsArray) {
                processMonitorCityTypeRelationShip.put(TaskHelper.TAG_GONGJIJIN_LOAD + gjj_loan_ie.trim(), TaskHelper.TAG_GONGJIJIN_LOAN_IE);
            }
        }

        String shebao_ie_cityids = PropertiesUtils.getProperties(TaskHelper.SHEBAO_IE_CITYIDS, "");
        if (StringUtils.isNotEmpty(shebao_ie_cityids)) {
            String[] shebao_ie_cityidsArray = shebao_ie_cityids.split(",");
            for (String sb_ie : shebao_ie_cityidsArray) {
                processMonitorCityTypeRelationShip.put(TaskHelper.TAG_SHEBAO + sb_ie.trim(), TaskHelper.TAG_SHEBAO_IE);
            }
        }
    }

    public static String getTaskNotifyTag(Task t) {
        if (t == null) return "";
        Class clazz = t.getClass();
        if (clazz == SheBaoTask.class) {
            return TAG_SHEBAO_NOTIFY;
        } else if (clazz == ShebaoYanzhengmaTask.class) {
            return TAG_SHEBAO_YZM_NOTIFY;
        } else if (clazz == GjjTask.class) {
            return TAG_GONGJIJIN_NOTIFY;
        } else if (clazz == GjjCodeTask.class) {
            return TAG_GONGJIJIN_YZM_NOTIFY;
        } else if (clazz == GjjLoanTask.class) {
            return TAG_GONGJIJIN_LOAD_NOTIFY;
        } else if (clazz == GjjLoanCodeTask.class) {
            return TAG_GONGJIJIN_LOAD_YZM_NOTIFY;
        } else if (clazz == FaYuanTask.class) {
            return TAG_FAYUAN_NOTIFY;
        }
        return "";
    }

    /**
     * 根据taskname获取taskclass
     *
     * @param tableName
     * @return
     */
    public static Class getTaskClassByTableName(String tableName) {
        if (StringUtils.isEmpty(tableName)) {
            return null;
        }
        if (TableUtils.SHEBAO_TASK_TABLE.equals(tableName)) {
            return SheBaoTask.class;
        } else if (TableUtils.SHEBAO_YZM_TASK_TABLE.equals(tableName)) {
            return ShebaoYanzhengmaTask.class;
        } else if (TableUtils.GJJ_YZM_TASK_TABLE.equals(tableName)) {
            return GjjCodeTask.class;
        } else if (TableUtils.GJJ_TASK_TABLE.equals(tableName)) {
            return GjjTask.class;
        } else if (TableUtils.GJJ_LOAN_TASK_TABLE.equals(tableName)) {
            return GjjLoanTask.class;
        } else if (TableUtils.GJJ_LOAN_YZM_TASK_TABLE.equals(tableName)) {
            return GjjLoanCodeTask.class;
        } else if (TableUtils.FAYUAN_TASK_TABLE.equals(tableName)) {
            return FaYuanTask.class;
        }
        return null;
    }

    public static String getTaskTag(Class<? extends Task> clazz) {
        if (clazz == SheBaoTask.class) {
            return TAG_SHEBAO;
        } else if (clazz == ShebaoYanzhengmaTask.class) {
            return TAG_SHEBAO_YZM;
        } else if (clazz == GjjTask.class) {
            return TAG_GONGJIJIN;
        } else if (clazz == GjjCodeTask.class) {
            return TAG_GONGJIJIN_YZM;
        } else if (clazz == GjjLoanTask.class) {
            return TAG_GONGJIJIN_LOAD;
        } else if (clazz == GjjLoanCodeTask.class) {
            return TAG_GONGJIJIN_LOAD_YZM;
        } else if (clazz == FaYuanTask.class) {
            return TAG_FAYUAN;
        }
        return "";
    }

    public static String getProcessMonitorType(String type, Integer cityId) {
        if (cityId == null)
            return type;

        if (processMonitorCityTypeRelationShip.containsKey(type + cityId)) {
            return processMonitorCityTypeRelationShip.get(type + cityId);
        }

        return type;
    }

    public static String getTaskTag(Task t) {
        if (t == null) return "";
        Class clazz = t.getClass();
        String type = getTaskTag(clazz);
        return getProcessMonitorType(type, t.getCityId());
    }

    /***
     * JSON字符串转化为Netty中的ByteBuf
     * @param task
     * @return
     */
    public static ByteBuf encode(Task task) {
        if (task == null) return null;
        String result = JSONHelper.toJSONString(task);
        ByteBuf buf = null;
        byte[] byteBuf = result.getBytes(UTF_8);
        buf = PooledByteBufAllocator.DEFAULT.directBuffer(byteBuf.length);
        buf.writeBytes(byteBuf);
        return buf;
    }

    public static <T> T decode(String jsonStr, String type) {
        if (StringUtils.isEmpty(jsonStr) || StringUtils.isEmpty(type)) {
            return null;
        }
        Class<T> clazz = getClassNameByType(type);

        if (clazz == null) {
            throw new RuntimeException("type not support");
        }
        return decode(jsonStr, clazz);
    }

    public static String getTaskKey(Task task, String tag) {
        if (task == null) {
            return "";
        }
        String key = "tag:" + tag + "-taskId:" + task.getId() + "-cityId:" + task.getCityId() + (task.getCurrentStep() == null ? "" : "-step:" + task.getCurrentStep());
        return key;
    }

    public static <T> T decode(String jsonStr, Class<T> clazz) {
        if (clazz == null || StringUtils.isEmpty(jsonStr)) {
            return null;
        }
        T result = JSON.parseObject(jsonStr, clazz);
        return result;
    }

    public static String getTableNameByTaskType(Task task) {
        return getTableNameByTaskType(task.getClass());
    }

    public static <T> String getTableNameByTaskType(Class clazz) {
        return TableUtils.getTableName(clazz);
    }

    public static Class getClassNameByType(String type) {
        Class clazz = null;
        if (TaskHelper.TAG_SHEBAO.equals(type) || TaskHelper.TAG_SHEBAO_NOTIFY.equals(type) || TaskHelper.TAG_SHEBAO_IE.equals(type)) {
            clazz = SheBaoTask.class;
        } else if (TaskHelper.TAG_SHEBAO_YZM.equals(type) || TaskHelper.TAG_SHEBAO_YZM_NOTIFY.equals(type)) {
            clazz = ShebaoYanzhengmaTask.class;
        } else if (type.equals(TaskHelper.TAG_GONGJIJIN) || TaskHelper.TAG_GONGJIJIN_NOTIFY.equals(type) || TaskHelper.TJ_GONGJIJIN.equals(type) || TaskHelper.TAG_GONGJIJIN_IE.equals(type)) {
            clazz = GjjTask.class;
        } else if (TaskHelper.TAG_GONGJIJIN_YZM.equals(type) || TaskHelper.TAG_GONGJIJIN_YZM_NOTIFY.equals(type)) {
            clazz = GjjCodeTask.class;
        } else if (TaskHelper.TAG_GONGJIJIN_LOAD.equals(type) || TaskHelper.TAG_GONGJIJIN_LOAD_NOTIFY.equals(type) || TaskHelper.TAG_GONGJIJIN_LOAN_IE.equals(type)) {
            clazz = GjjLoanTask.class;
        } else if (TaskHelper.TAG_GONGJIJIN_LOAD_YZM.equals(type) || TaskHelper.TAG_GONGJIJIN_LOAD_YZM_NOTIFY.equals(type)) {
            clazz = GjjLoanCodeTask.class;
        } else if (TaskHelper.TAG_FAYUAN.equals(type) || TaskHelper.TAG_FAYUAN_NOTIFY.equals(type)) {
            clazz = FaYuanTask.class;
        }

        return clazz;
    }
}
